package com.quanquan.store.controller;

import DTO.PageDTO;
import com.quanquan.store.dto.BannerDTO;
import com.quanquan.store.request.AdsenceRequest;
import com.quanquan.store.request.BannerRequest;
import com.quanquan.store.service.BannerService;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import utils.StringUtils;
import web.response.ResultBody;

import java.util.List;

/**
 * @ClassName:BannerController
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/3/18 18:16
 * @Version 1.0
 */
@RestController
@RequestMapping("/banner")
public class BannerController {

    private final BannerService bannerService;

    @Autowired
    public BannerController(BannerService bannerService) {
        this.bannerService = bannerService;
    }

    /**
     * 添加banner
     *
     * @param request
     * @return store
     */
    @PostMapping("/add_banner")
    public ResultBody addBanner(@RequestBody BannerRequest request) {
        boolean flag = bannerService.saveBanner(request);
        if (flag) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS");
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
    }


    /**
     * 删除banner
     *
     * @param request
     * @return
     */
    @PostMapping("/remove_banner")
    public ResultBody removeBanner(@RequestBody BannerRequest request) {
        if (StringUtils.isEmpty(request.getId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "未指明要删除的bannerid");
        }
        boolean flag = bannerService.removeBanner(request);
        if (flag) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS");
        } else {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
        }
    }

    /**
     * 上下架banner
     *
     * @param request
     * @return
     */
    @PostMapping("/update_status")
    public ResultBody updateStatus(@RequestBody BannerRequest request) {
        if (StringUtils.isEmpty(request.getId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "bannerId不可为空");
        }
        boolean flag = bannerService.updateStatus(request);
        if (flag) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS");
        } else {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
        }
    }

    /**
     * 查找未下架banner列表
     *
     * @param request
     * @return
     */
    @PostMapping("/query_list")
    @ResponseBody
    public ResultBody queryList(@RequestBody BannerRequest request) {
        List<BannerDTO> bannerDTOList = bannerService.queryList(request);
        if (bannerDTOList.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无banner列表");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", bannerDTOList);
    }


    /**
     * banner详情
     *
     * @param request
     * @return
     */
    @PostMapping("/query_by_id")
    public ResultBody queryById(@RequestBody BannerRequest request) {
        if (StringUtils.isEmpty(request.getId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "输入的bannerid为空");
        }
        BannerDTO bannerDTO = bannerService.query(request.getId());
        if (bannerDTO == null) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无此banner信息");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", bannerDTO);
    }


    /**
     * 修改banner信息
     *
     * @param request
     * @return
     */
    @PostMapping("update_banner")
    public ResultBody updateBanner(@RequestBody BannerRequest request) {
        boolean flag = bannerService.updateBanner(request);
        if (flag) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "更新成功");
        } else {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "更新失败");
        }
    }

    /**
     * 通过广告类型，广告位查找banner列表
     *
     * @param request
     * @return
     */
    @PostMapping("/query_list_by_banner_type")
    public ResultBody queryListByBannerType(@RequestBody AdsenceRequest request) {
        if (request.getBannerType() == null) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "未指明广告位类型");
        }
        List<BannerDTO> bannerDTOList = bannerService.queryListByBannerType(request);
        if (bannerDTOList.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无数据");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", bannerDTOList);
    }


    /**
     * 通过广告类型，广告位查找banner列表
     *
     * @param request
     * @return
     */
    @PostMapping("/query_all_list_by_adsence_id")
    public ResultBody queryAllListByBannerType(@RequestBody BannerRequest request) {
        List<BannerDTO> bannerDTOList = bannerService.queryByAdsenseId(request);
        if (bannerDTOList.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无数据");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", bannerDTOList);
    }

    /**
     * 多条件查询
     *
     * @param request
     * @return
     */
    @PostMapping("/query_list_manage")
    public ResultBody queryByCondition(@RequestBody BannerRequest request) {
        PageDTO<BannerDTO> bannerPageDTO = bannerService.queryByCondition(request);
        if (null != bannerPageDTO && bannerPageDTO.getList().size() > 0) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), bannerPageDTO);
        }
        return new ResultBody(ResCodeEnum.NO_DATA.getCode(), ResCodeEnum.NO_DATA.getMsg());

    }

}
